package com.unsyied.mbg.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 身份证生成器
 */
public class IDGenerator {


    public static String[] pro = {"11", "12", "13", "14", "15"
            , "21", "22", "23"
            , "31", "32", "33", "34", "35", "36", "37"
            , "41", "42", "43", "44", "45", "46"
            , "50", "51", "52", "53", "54"
            , "61", "62", "63", "64", "65"
            , "71"
            , "81", "82"
            , "91"};
    public static String[] yearFront = {"19", "20"};
    public static String[] month = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
    public static String[] code = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};

    public static String proS;
    public static String cityS;
    public static String xianS;
    public static String yearFS;
    public static String yearBS;
    public static String monthS;
    public static String dayS;
    public static String policeS;
    public static String sexS;
    public static String codeS;

    public static void main(String[] args) {
        System.out.println(random());
    }

    public static String random() {

        String id = "";
        while (id.length() != 18) {

            int pros = (int) (Math.random() * 35);
            proS = pro[pros];
//        System.out.println(pro[pros]);
            int city = (int) (Math.random() * 20);
            String city1 = city + "";
            if (city < 10) {
                city1 = "0" + city1;
            }
            cityS = city1;
//        System.out.println(city1);
            int xian = (int) (Math.random() * 35);
            String xians = xian + "";
            if (xian < 10) {
                xians = "0" + xian;
            }
            xianS = xians;
//        System.out.println(xians);
            int yearFronts = (int) (Math.random() * 2);
            yearFS = yearFront[0];
            String year = "2021";
            while (2022 - Integer.parseInt(year) > 55 || 2022 - Integer.parseInt(year) < 25) {
//        System.out.println(yearFront[yearFronts]);
//            if (yearFront[yearFronts].equals("20")) {
//                int year1 = (int) (Math.random() * 4);
//                String year11 = year1 + "";
//                if (year1 < 10) {
//                    year11 = "0" + year1;
//                }
//                yearBS = year11;
////            System.out.println(year11);
//            } else {
                int year1 = (int) (Math.random() * 100);
                String year11 = year1 + "";
                if (year1 < 10) {
                    year11 = "0" + year1;
                }
                yearBS = year11;
//            System.out.println(year11);
//            }
                year = yearFS + yearBS;
            }
            int months = (int) (Math.random() * 12);
            monthS = month[months];
//        System.out.println(month[months]);
            if (months == 0 || months == 2 || months == 4 || months == 6 || months == 7 || months == 9 || months == 11) {

                int day = (int) (Math.random() * 31);
                String days = day + "";
                if (day < 10) {
                    days = "0" + day;
                }
                dayS = days;
//            System.out.println(days);
            } else if (months == 1 && Integer.parseInt(monthS) % 4 != 0) {
                int day = (int) (Math.random() * 27);
                String days = day + "";
                if (day < 10) {
                    days = "0" + day;
                }
                dayS = days;
//            System.out.println(days);
            } else {
                int day = (int) (Math.random() * 30);
                String days = day + "";
                if (day < 10) {
                    days = "0" + day;
                }
                dayS = days;
            }
            int police = (int) (Math.random() * 35);
            String policez = xian + "";
            if (police < 10) {
                policez = "0" + police;
            }
            policeS = policez;
            int sex = (int) (Math.random() * 10);
            sexS = sex + "";


            String codeSource = proS + cityS + xianS + yearFS + yearBS + monthS + dayS + policeS + sexS;
            String[] split = codeSource.split("");
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < split.length; i++) {
                switch (i) {
                    case 0:
                        list.add(Integer.parseInt(split[i]) * 7);
                        break;
                    case 1:
                        list.add(Integer.parseInt(split[i]) * 9);
                        break;
                    case 2:
                        list.add(Integer.parseInt(split[i]) * 10);
                        break;
                    case 3:
                        list.add(Integer.parseInt(split[i]) * 5);
                        break;
                    case 4:
                        list.add(Integer.parseInt(split[i]) * 8);
                        break;
                    case 5:
                        list.add(Integer.parseInt(split[i]) * 4);
                        break;
                    case 6:
                        list.add(Integer.parseInt(split[i]) * 2);
                        break;
                    case 7:
                        list.add(Integer.parseInt(split[i]) * 1);
                        break;
                    case 8:
                        list.add(Integer.parseInt(split[i]) * 6);
                        break;
                    case 9:
                        list.add(Integer.parseInt(split[i]) * 3);
                        break;
                    case 10:
                        list.add(Integer.parseInt(split[i]) * 7);
                        break;
                    case 11:
                        list.add(Integer.parseInt(split[i]) * 9);
                        break;
                    case 12:
                        list.add(Integer.parseInt(split[i]) * 10);
                        break;
                    case 13:
                        list.add(Integer.parseInt(split[i]) * 5);
                        break;
                    case 14:
                        list.add(Integer.parseInt(split[i]) * 8);
                        break;
                    case 15:
                        list.add(Integer.parseInt(split[i]) * 4);
                        break;
                    case 16:
                        list.add(Integer.parseInt(split[i]) * 2);
                        break;
                }
            }

            int sum = list.stream().reduce(Integer::sum).orElse(0);
            codeS = code[sum % 11];
            id = codeSource + codeS;
        }
//        System.out.println(codeSource + codeS);
//        System.out.println(yearFS+yearBS);
        return id;
    }


}
